#include "dashboard2.h"
#include <QDebug>

DashBoard2::DashBoard2(QWidget *parent) : QWidget(parent)
{
    max_speed_ = 120;
    min_speed_ = 0;
    start_angle_ = -65.0;
    end_angle_ = 245.0;
    scale_step_ = 5;
    scale_cout_ = (max_speed_ - min_speed_)/scale_step_;
    realtime_speed_ = 0;
    rec_speed_yellow_ = 60;
    eb_speed_red_ = 70;
    //default color
    out_circle_color_ = QColor(255,0,0);
    speed_unit_color_ = QColor(255,255,255);
    center_circle_color_ = QColor(255,255,255);
    scale_color_ = QColor(255,255,255);
    indicatior_color_ = QColor(255,255,255);
    speed_color_ = QColor(255,255,255);
    scale_num_color_ = QColor(255,255,255);

    setEBSpeed(true,80);
    setRecSpeed(true,60);
}

void DashBoard2::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.fillRect(QRectF(0,0,this->width(),this->height()),Qt::transparent);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.scale(this->width()/360.0,this->height()/360.0);
    painter.translate(180.0,180.0);
    painter.setPen(Qt::NoPen);
    drawOutCircle(&painter);
    drawScale(&painter);
    drawIndicator(&painter);
    drawScaleNum(&painter);
    drawYellowCursor(&painter);
    drawRedCursor(&painter);
    drawCenterCircle(&painter);
    drawSpeedValue(&painter);
}

void DashBoard2::drawYellowCursor(QPainter *painter)
{
    painter->save();
    float angleStep = (end_angle_ - start_angle_)/(max_speed_ - min_speed_);
    if(red_cursor_visible_ && !standby_mode_)
    {
        painter->setPen(QColor(255,255,0));
        painter->setBrush(QColor(255,255,0));
    }
    else
    {
        painter->setPen(QColor(0,0,0));
        painter->setBrush(QColor(0,0,0));
    }
    painter->rotate(start_angle_ + angleStep*rec_speed_yellow_);
    QPolygon pts;
    pts.setPoints(3,-170,-10,-170,10,-150,0);
    painter->drawConvexPolygon(pts);
    painter->restore();
}

void DashBoard2::drawRedCursor(QPainter *painter)
{
    painter->save();
    float angleStep = (end_angle_ - start_angle_)/(max_speed_ - min_speed_);
    if(yellow_cursor_visible_ && !standby_mode_)
    {
        painter->setPen(QColor(255,0,0));
        painter->setBrush(QColor(255,0,0));
    }
    else
    {
        painter->setPen(QColor(0,0,0));
        painter->setBrush(QColor(0,0,0));
    }
    painter->rotate(start_angle_ + angleStep*eb_speed_red_);
    QPolygon pts;
    pts.setPoints(3,-170,-10,-170,10,-150,0);
    painter->drawConvexPolygon(pts);
    painter->restore();
}

void DashBoard2::drawOutCircle(QPainter *painter)
{
    painter->save();
    painter->setPen(out_circle_color_);
    QPen pen= painter->pen();
    pen.setWidth(2);
    painter->setPen(pen);
    QRectF rectangle(-150.0, -150.0, 300.0, 300.0);
    int startAngle = -65 * 16;
    int spanAngle = 310 * 16;
    painter->drawArc(rectangle, startAngle, spanAngle);

    painter->setPen(speed_unit_color_);
    QFont font("Arial",20,QFont::Normal);
    painter->setFont(font);
    painter->drawText(QRect(-90,120,180,60),Qt::AlignCenter,"km/h");
    painter->restore();
}

void DashBoard2::drawCenterCircle(QPainter *painter)
{
    painter->save();
    painter->setPen(center_circle_color_);
    QPen pen= painter->pen();
    pen.setWidth(3);
    painter->setPen(pen);
    painter->setBrush(Qt::black);
    QRectF rectangle(-30.0, -30.0, 60.0, 60.0);
    painter->drawEllipse(rectangle);
    painter->restore();
}

void DashBoard2::drawSpeedValue(QPainter *painter)
{
    painter->save();
    painter->setPen(speed_color_);
    QFont font("Arial",20,QFont::Normal);
    painter->setFont(font);
    QRectF rectangle(-30.0, -30.0, 60.0, 60.0);
    painter->drawText(rectangle,Qt::AlignCenter,QString().setNum(realtime_speed_));
    painter->restore();
}

void DashBoard2::drawScale(QPainter *painter)
{
    painter->save();
    float angleStep = (end_angle_ - start_angle_)/scale_cout_;
    painter->setPen(scale_color_);
    painter->rotate(start_angle_);
    for(int i=0;i<=scale_cout_;i++)
    {
        if(i%2 == 0)
        {
            painter->setPen(QPen(scale_color_,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
            painter->drawLine(-148,0,-133,0);
        }
        else
        {
            painter->setPen(QPen(scale_color_,2,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));
            painter->drawLine(-148,0,-138,0);
        }
        painter->rotate(angleStep);
    }
    painter->restore();
}

void DashBoard2::drawIndicator(QPainter *painter)
{
    painter->save();
    float angleStep = (end_angle_ - start_angle_)/(max_speed_ - min_speed_);
    painter->setPen(indicatior_color_);
    painter->rotate(start_angle_ + angleStep*realtime_speed_);
    QPen pen= painter->pen();
    pen.setWidth(2);
    painter->setPen(pen);
    painter->drawLine(-128,0,-100,0);
    painter->setBrush(indicatior_color_);
    painter->drawRoundedRect(-100,-6,80,12,25,6);
    painter->restore();
}

void DashBoard2::drawScaleNum(QPainter *painter)
{
    painter->save();
    QFont font("Arial",15,QFont::Normal);
    painter->setFont(font);
    painter->setPen(scale_num_color_);

    int scale_major_cout = scale_cout_ /2;
    double start_rad = (3.14*2*245.0)/360.0;
    //qDebug()<<"start rad:"<<start_rad;
    QFontMetricsF fm(font);
    double deltaRad = 2*3.14/(scale_major_cout+2);
    //qDebug()<<"delta rad:"<<deltaRad;
    double w,h,tmpVal;
    int x,y;
    QString str;

    for(int i=0;i<scale_major_cout+1;i++)
    {
        tmpVal = 1.0*i*((max_speed_ - min_speed_)/scale_major_cout)+min_speed_;
        str = QString("%1").arg(tmpVal);
        w=fm.size(Qt::TextSingleLine,str).width();
        h=fm.size(Qt::TextSingleLine,str).height();
        x=110*cos(start_rad - deltaRad*i)-w/2;
        y=-110*sin(start_rad - deltaRad*i)+h/4;
//        qDebug()<<"rad:"<<start_rad - deltaRad*i<<"x:"<<x<<"y:"<<y;
        painter->drawText(x,y,str);
    }
    painter->restore();
}

void DashBoard2::setEBSpeed(bool visible, float eb_speed)
{
    yellow_cursor_visible_ = visible;
    eb_speed_red_ = eb_speed;
    update();
}

void DashBoard2::setRecSpeed(bool visible,float rec_speed)
{
    red_cursor_visible_ = visible;
    rec_speed_yellow_ = rec_speed;
    update();
}

void DashBoard2::setRealTimeSpeed(float speed)
{
    realtime_speed_ = speed;
    update();
}

void DashBoard2::setStandByMode(bool set_mode)
{
    if(set_mode)
    {
        out_circle_color_ = QColor(0,0,0);
        speed_unit_color_ = QColor(0,0,0);
        indicatior_color_ = QColor(0,0,0);
        standby_mode_ = true;
        speed_color_ = QColor(0,0,0);
    }
    else
    {
        out_circle_color_ = QColor(255,0,0);
        speed_unit_color_ = QColor(255,255,255);
        indicatior_color_ = QColor(255,255,255);
        standby_mode_ = false;
        speed_color_ = QColor(255,255,255);
    }
    update();
}
